package com.car.carmgr.service.impl;

import com.car.carmgr.bean.News;
import com.car.carmgr.mapper.NewsMapper;
import com.car.carmgr.service.NewsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NewsServiceImpl implements NewsService {

    @Autowired
    private NewsMapper newsMapper;

    @Override
    public int insertNews(News news) {
        return newsMapper.insertNews(news);
    }

    @Override
    public int deleteNewsById(Integer newsId) {
        return newsMapper.deleteNewsById(newsId);
    }

    @Override
    public int updateNews(News news) {
        return newsMapper.updateNews(news);
    }

    @Override
    public PageInfo<News> findNewsByWhere(String title, String content, String publisherName, Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        List<News> list = newsMapper.findNewsByWhere(title,content,publisherName);
        PageInfo<News> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public Integer getCountByWhere(String title, String content, String publisherName) {
        return newsMapper.getCountByWhere(title,content,publisherName);
    }

    @Override
    public int deleteSomeNews(String[] split) {
        int row = 0;
        if(split != null && split.length != 0){

            for(String id : split){
                row += newsMapper.deleteNewsById(Integer.parseInt(id));
            }
        }
        return row;
    }

    @Override
    public News findNewsById(Integer id) {
        return newsMapper.findNewsById(id);
    }
}
